/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "WLOGR_main.h"
#include <LOGR_logs.h>
#include <timek.h>
#include <rep_general_logml.h>
#include <rep_logs_general.h>


int
read_graph_file_and_set_frame(mainFrame *mf);

void
process_arguments(mainFrame *mf);

char *
get_complete_filename(char *filename,const char *ldir);

void
wegloggr_version_info(void)
{
    fprintf(stderr,WEBLOGGR_VERSION_INFO);
    fprintf(stderr,"\n\n");
}

void
wegloggr_help_info(void)
{
    fprintf(stderr,WEBLOGGR_VERSION_INFO);
    fprintf(stderr,WEBLOGGR_HELP_INFO);
    fprintf(stderr,"\n\n");
}

int
main(int argc, char *argv[])
{
  mainFrame *mf = NULL;

  if(argc <= 1)
  {	
      fprintf(stderr,"No arguments, try webloggr -h\n");
      return 0;
  }
      
  mf = new_main_frame();
  assert(mf != NULL);
  LOGR_start_application();

  if(read_command_line(mf,argc,argv) == 0)
    {
	process_arguments(mf);
	/*printf("GRAPH FILE %s\n", mf->graph_file ? mf->graph_file : "NULL");
	printf("LML FILE %s\n", mf->logml_file ? mf->logml_file : "NULL");
	printf("LOG FILE %s\n", mf->log_file ? mf->log_file : "NULL");
	printf("LOG ROOT DIR %s\n", mf->log_root_dir ? mf->log_root_dir : "NULL");
	printf("WEB SERVER NAME %s\n", mf->web_server_name ? mf->web_server_name : "NULL");
	printf("URI PREFIX %s\n", mf->uri_prefix ? mf->uri_prefix : "NULL");*/
    }

  if(mf)
      delete_main_frame(mf);
  LOGR_end_application();
  return 0;
}

void
delete_main_frame(mainFrame *mf)
{
  LNode_type *ptr;

  if(mf->ltgr_frame)
    {
      for(ptr = mf->ltgr_frame->back; ptr != mf->ltgr_frame ; ptr = ptr->back)
	{
	  tgrFrame *tgf = (tgrFrame *) ptr->info;
	  if(tgf)
	    delete_tgr_frame(tgf);
	}
    }
  
  if(mf->ltgr_frame)
    Delete_all_list(mf->ltgr_frame);
  if(mf->basename) {
      free(mf->basename); mf->basename = NULL;
  }
  if(mf->wsn) {
      delete_web_stat(mf->wsn); mf->wsn = NULL;
  }

  mf->cnp = NULL;

  if(mf->graph_file) {
      free(mf->graph_file); mf->graph_file = NULL;
  }
  if(mf->logml_file) {
      free(mf->logml_file); mf->logml_file = NULL;
  }
  if(mf->log_file) {
      free(mf->log_file); mf->log_file = NULL;
  }
  if(mf->log_root_dir) {
      free(mf->log_root_dir); mf->log_root_dir = NULL;
  }
  if(mf->web_server_name) {
      free(mf->web_server_name); mf->web_server_name = NULL;
  }
  if(mf->local_domain) {
      free(mf->local_domain); mf->local_domain = NULL;
  }
  if(mf->ncidr > 0)
  {
      int i = 0;
      for(i=0;i<mf->ncidr;i++)
      {
	  if(mf->array_cidr[i]) {
	      free(mf->array_cidr[i]); mf->array_cidr[i] = NULL;
	  }
      }
      mf->ncidr=0;
  }
  if(mf->log_prefix) {
      free(mf->log_prefix); mf->log_prefix = NULL;
  }
  if(mf->report_prefix) {
      free(mf->report_prefix); mf->report_prefix = NULL;
  }
  if(mf->report_dir) {
      free(mf->report_dir); mf->report_dir = NULL;
  }

  free(mf); mf = NULL;
}

mainFrame *
new_main_frame(void)
{
  GraphFrame *gf = create_current_graph();
  tgrFrame *tgf = (gf ? new_tgr_frame(gf) : NULL);
  mainFrame *mf = (mainFrame *)calloc(1, sizeof(mainFrame));
  if(!mf) {
      fprintf(stderr,"Out of Memory new_main_frame\n");
      abort();
  }
  init_graph(gf);
  mf->cur = 0;
  mf->nroot = NULL;
  mf->ltgr_frame = init_linked_list();  
  mf->findex = 0;
  mf->wsn = new_web_stat();
  mf->cnp = NULL;

  mf->ncidr = 0;
  mf->graph_file = NULL;
  mf->log_file = NULL;
  mf->logml_file = NULL;
  mf->log_root_dir = NULL;
  mf->web_server_name = NULL;
  mf->local_domain = NULL;
  mf->log_prefix = NULL;
  mf->report_prefix = NULL;
  mf->report_dir = NULL;
  mf->log_type = -1;
  mf->compute_users = -1;
  mf->nof = -1;
  mf->rhip = -1;
  mf->rhip_timeout = -1;
  mf->num_elements = -1;
  mf->num_keywords = -1;

/* 
  mf->num_users = DEFAULT_NUMBER_USERS;
  mf->max_edges = DEFAULT_MAXIMUM_EDGES;
  mf->num_graphs = DEFAULT_NUMBER_GRAPHS;
  mf->min_edges = DEFAULT_MINIMUM_EDGES;
*/
  
  mf->num_users = -1;
  mf->max_edges = -1;	
  mf->num_graphs = -1;
  mf->min_edges = -1;

  LOGR_set_logs_directory(mf->wsn, DEFAULT_LOG_ROOT_DIRECTORY);
  LOGR_set_base_webhost(mf->wsn, DEFAULT_WEB_SERVER);
  LOGR_set_logs_filetype(mf->wsn, LOGR_EXTENDED_LOG);
  LOGR_set_local_domain(mf->wsn,DEFAULT_LOCAL_DOMAIN);
  LOGR_add_cidr(mf->wsn,DEFAULT_SUBNET_ADDRESS1);
  LOGR_add_cidr(mf->wsn,DEFAULT_SUBNET_ADDRESS2);
  
  if(tgf)
    {
      Insert_linked_list(mf->ltgr_frame, create_new_node_llist((char *)tgf));
      mf->cur_node_tgf = mf->ltgr_frame->back;
    }
  return mf;
}

int
read_command_line(mainFrame *mf, int argc, char *argv[])
{
  int mes = 0;
  int arg = 0;

  for (arg=1; arg<argc; arg++) {
      if (*argv[arg] == '-') 
      {
	  if (!strcmp(argv[arg], "-gf")) 
	  {
	      mf->graph_file = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->graph_file,argv[++arg]) : 
		  DEFAULT_GRAPH_FILE;
	  }
	  else if (!strcmp(argv[arg], "-conf"))
	  {
	      mf->conf_file = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->conf_file,argv[++arg]) : 
		  DEFAULT_XML_CONF_FILE;
	  }
	  else if (!strcmp(argv[arg], "-logf") ||
		   !strcmp(argv[arg], "-l")) 
	  {
	      mf->log_file = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->log_file,argv[++arg]) : 
		  DEFAULT_EXTENDED_LOG_FILE;
	  }
	  else if (!strcmp(argv[arg], "-lmlf") ||
		   !strcmp(argv[arg], "-o"))
	  {
	      mf->logml_file = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->logml_file,argv[++arg]) : 
		  DEFAULT_LOGML_FILE;
	  }
	  else if (!strcmp(argv[arg],"-logroot")) 
	  {
	      mf->log_root_dir = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->log_root_dir,argv[++arg]) : 
		  DEFAULT_LOG_ROOT_DIRECTORY;
	  }
	  else if (!strcmp(argv[arg],"-webserver")) 
	  {
	      mf->web_server_name = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->web_server_name,argv[++arg]) : 
		  DEFAULT_WEB_SERVER;
	  }
	  else if (!strcmp(argv[arg],"-domain")) 
	  {
	      mf->local_domain = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->local_domain,argv[++arg]) : 
		  DEFAULT_LOCAL_DOMAIN;
	  }
	  else if (!strcmp(argv[arg],"-subnet")) 
	  {
	      if(mf->ncidr < MAX_CIDR) {
		  mf->array_cidr[mf->ncidr] = 
		      (arg+1 < argc && *argv[arg+1] != '-') ?
		      GRStrAllocCopy(mf->array_cidr[mf->ncidr],argv[++arg]) : 
		      NULL;
		  mf->ncidr++;
	      }
	  }
	  else if (!strcmp(argv[arg],"-prefix")) 
	  {
	      mf->uri_prefix = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->uri_prefix,argv[++arg]) : 
		  NULL;
	  }
	  else if (!strcmp(argv[arg],"-report_prefix") ||  
		   !strcmp(argv[arg],"-rp"))
	  {
	      mf->report_prefix = (arg+1 < argc && *argv[arg+1] != '-') ?
		  GRStrAllocCopy(mf->report_prefix,argv[++arg]) : 
		  NULL;
	  }
	  else if (!strcmp(argv[arg],"-n")) 
	  {
	      mf->num_elements = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_NUMBER_ELEMENTS;
	  }
	  else if (!strcmp(argv[arg],"-nk")) 
	  {
	      mf->num_keywords = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_NUMBER_KEYWORDS;
	  }
	  else if (!strcmp(argv[arg], "-rhip")) 
	  {
	      mf->rhip = 1;
	  }
	  else if (!strcmp(argv[arg],"-rhip_timeout")) 
	  {
	      mf->rhip_timeout = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_RESOLVE_TIMEOUT;
	      if(mf->rhip_timeout < 0)
		  mf->rhip_timeout = 0;
	  }
	  else if (!strcmp(argv[arg],"-num_graphs")) 
	  {
	      mf->num_graphs = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_NUMBER_GRAPHS;
	  }
	  else if (!strcmp(argv[arg],"-num_users")) 
	  {
	      mf->num_users = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_NUMBER_USERS;
	  }
	  else if (!strcmp(argv[arg],"-min_edges")) 
	  {
	      mf->min_edges = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_MINIMUM_EDGES;
	  }
	  else if (!strcmp(argv[arg],"-max_edges")) 
	  {
	      mf->max_edges = (arg+1 < argc && *argv[arg+1] != '-') ?
		  atoi(argv[++arg]) :
		  DEFAULT_MAXIMUM_EDGES;
	  }
	  else if (!strcmp(argv[arg], "-cus")) 
	  {
	      mf->compute_users = 1;
	  }
	  else if (!strcmp(argv[arg], "-x")) 
	  {
	      mf->log_type = LOGR_EXTENDED_LOG;
	  }
	  else if (!strcmp(argv[arg], "-c")) 
	  {
	      mf->log_type = LOGR_COMMON_LOG;
	  }
	  else if (!strcmp(argv[arg], "-h") || !strcmp(argv[arg], "-?"))
	  {
	      wegloggr_help_info();
	      return -1;
	  }
	  else if (!strcmp(argv[arg], "-version") || 
		   !strcmp(argv[arg], "-v"))
	  {
	      wegloggr_version_info();
	      return -1;
	  }
	  else 
	  {
	      fprintf(stderr,"Wrong Argument %s\n", argv[arg]);
	      return -1;
	  }
      }
      else  /* If no leading `-' then check for log file */
      {	
	  if(mf->log_file)
	      fprintf(stderr,"Log File Argument was set to %s\n",mf->log_file);
	  else
	      mf->log_file = GRStrAllocCopy(mf->log_file,argv[arg]);
      }
  }
  /*
  mes = read_graph_file_and_set_frame(mf);
  */
  return mes;
}

int
read_graph_file_and_set_frame(mainFrame *mf)
{
  char *text;
  int mes;
  char *filename = mf->graph_file;

  tgrFrame *tgf = (tgrFrame *)mf->cur_node_tgf->info;

  GraphFrame *gf = tgf->gf;

  if(!filename)
      return -1;

  if(!read_regular_file(filename, &text))
    {
      long len = strlen(text);
      GRStrAllocCopy(tgf->filename,filename);
      tgf->saved = 1;
      mf->basename = get_base_name(filename);
      
      if(mf->basename)
	printf("Basename is %s\n", mf->basename);
      else
	printf("No base name\n");

      if((mes = parse_xml_text_graph(gf,text,len)))
	{
	  if(mes < 0)
	    fprintf(stderr,"Graph File is wrong\n");
	}
      else
        {
	  if(get_graphic_flag(gf)!=GRAPHIC_ON)
	      set_shape_from_attributes(gf);
	}	
      tgf->root = get_root_vertex(gf);
      if(!tgf->root)
	  tgf->root = find_vertex_by_number(gf,1); /* First vertex */
      if(!tgf->root)
	  tgf->root = find_vertex_by_number(gf,2); /* Second vertex */

      LOGR_set_max_id_number(mf->wsn, get_num_vertex(gf));
      printf("Last vertex of graph %d\n", get_num_vertex(gf));
      
    }
  free(text); text = NULL;
  /*fclose(fp);*/
  return 1;
}

void
set_i_output_file(mainFrame *mf,ConfigNodePtr cnp,int i) 
{
    int nele = 0;
    int nk = 0;
    char *logml_file = NULL;
    char *report_prefix = NULL;
    char *report_dir = NULL;
    int num_users = 0;
    int max_edges = 0;
    int num_graphs = 0;
    int min_edges = 0;

    if(!mf || !cnp)
	return;

    nele = LOGR_get_config_nelements(cnp,i);
    nk = LOGR_get_config_nkeywords(cnp,i);
    logml_file = LOGR_get_config_logml_file(cnp,i);
    report_prefix = LOGR_get_config_report_prefix(cnp,i);
    report_dir = LOGR_get_config_report_dir(cnp,i);

    if(mf->compute_users == 1)
    {
	num_users = LOGR_get_config_num_users(cnp,i);
	max_edges = LOGR_get_config_max_edges(cnp,i);
	num_graphs = LOGR_get_config_num_graphs(cnp,i);
	min_edges = LOGR_get_config_min_edges(cnp,i);
    }

    if(mf->num_elements == -1 && nele != -1)
	mf->num_elements = nele;

    if(mf->num_keywords == -1 && nk != -1)
	mf->num_keywords = nk;

    if(!mf->logml_file && logml_file)
	GRStrAllocCopy(mf->logml_file,logml_file);

    if(!mf->report_prefix && report_prefix)
	GRStrAllocCopy(mf->report_prefix,report_prefix);

    if(!mf->report_dir && report_dir)
	GRStrAllocCopy(mf->report_dir,report_dir);

    if(mf->compute_users == 1)
    {
	if(mf->num_users == -1 && num_users != -1)
	    mf->num_users = num_users;
	if(mf->num_graphs == -1 && num_graphs != -1)
	    mf->num_graphs = num_graphs;
	if(mf->max_edges == -1 && max_edges != -1)
	    mf->max_edges = max_edges;
	if(mf->min_edges == -1 && min_edges != -1)
	    mf->min_edges = min_edges;
    }
}

void
process_configuration(mainFrame *mf,ConfigNodePtr cnp)
{
  char *graph_file = LOGR_get_config_graphfile_name(cnp);
  char *log_file = LOGR_get_config_logfile_name(cnp);
  char *log_prefix = LOGR_get_config_logfile_prefix(cnp);
  char *log_root_dir = LOGR_get_config_logfile_root_dir(cnp);
  char *web_server_name = LOGR_get_config_webserver_uri(cnp);
  char *local_domain = LOGR_get_config_webserver_domain(cnp);
  char *uri_prefix = LOGR_get_config_logfile_uri_prefix(cnp);
  int log_type = (int)LOGR_get_config_logfile_type(cnp);
  int rhip = (int)LOGR_get_config_logfile_res_ip(cnp);
  int rhip_timeout = LOGR_get_config_logfile_res_ip_timeout(cnp);
  LNode_type *lsubnets = LOGR_get_config_list_subnet(cnp);
  int nsn = LOGR_get_config_num_subnet(cnp);
  short cus = LOGR_get_config_compute_user_sessions(cnp);
  int nof = LOGR_get_config_num_output_files(cnp);


  if(mf->log_type == -1 && log_type != -1)
      mf->log_type = log_type;
  
  if(mf->rhip == -1 && rhip != -1)
      mf->rhip = rhip;

  if(mf->rhip_timeout == -1 && rhip_timeout != -1)
      mf->rhip_timeout = rhip_timeout; 

  if(mf->compute_users == -1 && cus != -1)
      mf->compute_users = cus;

  if(!mf->log_root_dir && log_root_dir)
      GRStrAllocCopy(mf->log_root_dir,log_root_dir);

  if(!mf->web_server_name && web_server_name)
      GRStrAllocCopy(mf->web_server_name,web_server_name);

  if(!mf->local_domain && local_domain)
      GRStrAllocCopy(mf->local_domain,local_domain);

  if(!mf->uri_prefix && uri_prefix)
      GRStrAllocCopy(mf->uri_prefix,uri_prefix);

  if(!mf->log_file && log_file)
      GRStrAllocCopy(mf->log_file,log_file);

  if(!mf->log_prefix && log_prefix)
      GRStrAllocCopy(mf->log_prefix,log_prefix);

  if(!mf->graph_file && graph_file)
      GRStrAllocCopy(mf->graph_file,graph_file);
  
  if(nsn > 0 && mf->ncidr == 0)
  {
      LNode_type *ptr = NULL;	
      int i = 0;
      for(ptr = lsubnets->back ; ptr != lsubnets ; ptr = ptr->back)
      {
	  char *scidr = (char *)ptr->info;
	  mf->array_cidr[i] = NULL;
	  if(i < MAX_CIDR)
	      GRStrAllocCopy(mf->array_cidr[i++],scidr);
      }
      mf->ncidr = i;
  }

  if(mf->nof == -1 && nof != -1)
      mf->nof = nof;
    
  if(nof > 0)
      set_i_output_file(mf,cnp,0);
}

void
process_conf_file(mainFrame *mf)
{
    char *filename = NULL;
    char *text = NULL;
    long len = 0;
    if(!mf)
	return;
    filename = mf->conf_file;

    if(!filename || filename[0]=='\0')
	return;
    
  if(!read_regular_file(filename, &text))
  {
      ConfigNodePtr cnp = new_config_node(); /* Delete it */
      len = strlen(text);
      LOGR_parse_xml_conf_file(cnp,text,len);
      mf->cnp = cnp;
      if(cnp)
	  process_configuration(mf,cnp);
  }
    
  if(text) {
      free(text); text = NULL;
  }
}

void
reset_output_variables(mainFrame *mf)
{
    mf->num_elements = -1;
    if(mf->logml_file) {
	free(mf->logml_file); mf->logml_file = NULL;
    }
    if(mf->report_prefix) {
	free(mf->report_prefix); mf->report_prefix = NULL;
    }
    if(mf->report_dir) {
	free(mf->report_dir); mf->report_dir = NULL;
    }
    mf->num_users = -1;
    mf->num_graphs = -1;
    mf->min_edges = -1;
    mf->max_edges = -1;
}

void
reset_and_set_new_output_files(mainFrame *mf,int i)
{
    ConfigNodePtr cnp = mf->cnp;
    reset_output_variables(mf);
    set_i_output_file(mf,cnp,i);
}

void
process_arguments(mainFrame *mf)
{
  const char *ldir = NULL;
  tgrFrame *tgf = (tgrFrame *)mf->cur_node_tgf->info;
  GraphFrame *gf = tgf->gf;
  int nof = 0;
  int iof = 0;

  if(mf->conf_file)
      process_conf_file(mf);

  nof = mf->nof > 0 ? mf->nof : 1;
  
  if(mf->log_type != -1)
      LOGR_set_logs_filetype(mf->wsn,mf->log_type); 

  if(mf->compute_users == 1)
      LOGR_set_compute_user_sessions(mf->wsn);
  else if(mf->compute_users == 0)
      LOGR_reset_compute_user_sessions(mf->wsn);

  if(mf->rhip == 1)
      LOGR_set_resolve_host_ip(mf->wsn);
  else if(mf->rhip == 0)
      LOGR_reset_resolve_host_ip(mf->wsn);

  if(mf->rhip_timeout > 0)
      LOGR_set_resolve_timeout(mf->wsn,mf->rhip_timeout);

  if(mf->graph_file)
      read_graph_file_and_set_frame(mf);

  if(mf->log_root_dir)
      LOGR_set_logs_directory(mf->wsn,mf->log_root_dir);
  if(mf->web_server_name)
      LOGR_set_base_webhost(mf->wsn,mf->web_server_name);
  if(mf->local_domain)
      LOGR_set_local_domain(mf->wsn,mf->local_domain);
  if(mf->ncidr > 0)
  {
      int i = 0;
      LOGR_clear_list_cidr(mf->wsn);
      for(i = 0 ; i<mf->ncidr; i++)
      {
	  if(i < MAX_CIDR)
	      LOGR_add_cidr(mf->wsn,mf->array_cidr[i]);
      }
  }
  if(mf->uri_prefix)
      LOGR_set_url_prefix(mf->wsn,mf->uri_prefix);

  ldir = mf ? LOGR_get_logs_directory(mf->wsn) : NULL;

    if(mf->log_file || mf->log_prefix)
    {
	char *cfilename = get_complete_filename(mf->log_file,ldir);
	char *ctfilename = get_complete_filename(mf->log_prefix,ldir);
	int nelements = 0;
	int nkeywords = 0;
	int ngr = 0;
	int mine = 0;
	int nus = 0;
	int maxe = 0;
	int mes = 0;	
	short cus = LOGR_get_compute_user_sessions(mf->wsn);
	int ext_log = LOGR_get_logs_filetype(mf->wsn);


	start_time();
	prn_time();
	if(cfilename)
	    mes = LOGR_read_tokens(gf, cfilename,mf->wsn);
	else if(ctfilename)
	{
	    GRStrAllocCat(ctfilename,"*");
	    mes = LOGR_read_all_files_templates(gf, ctfilename,mf->wsn);
	}
	prn_time();
	do {
	    printf("Generating Out File %d \n",iof+1);
	    if(cus && ext_log == LOGR_EXTENDED_LOG)
	    {
		ngr = mf->num_graphs;
		mine = mf->min_edges;
		nus = mf->num_users;
		maxe = mf->max_edges;
	    }
	    nelements = mf->num_elements > 0 ? mf->num_elements : 
		DEFAULT_NUMBER_ELEMENTS; 

	    nkeywords = mf->num_keywords > 0 ? mf->num_keywords : 
		DEFAULT_NUMBER_KEYWORDS; 

	    if(mf->logml_file && mes <= 0)
	    {
		report_logs_logml(gf, mf->wsn,mf->logml_file,nelements,
				  nkeywords,nus,maxe,mine);
	    }
	    if(mf->report_prefix && mes <= 0)
	    {
		report_index_of_log_reports(gf,mf->wsn,mf->report_prefix,
					    mf->report_dir,nelements,
					    nkeywords,ngr,mine,nus,maxe);
	    }
	    nof--;
	    iof++;
	    if(nof)
		reset_and_set_new_output_files(mf,iof);
	} while (nof);
    }
}


char *
get_complete_filename(char *filename,const char *ldir)
{
  char * cfilename = NULL;

  if(!filename)
      return NULL;

  if(filename[0]!='/' && ldir)
  {
      GRStrAllocCopy(cfilename, ldir);
      GRStrAllocCat(cfilename, "/");
      GRStrAllocCat(cfilename, filename);
  }
  else
      GRStrAllocCopy(cfilename,filename);

  return cfilename;
}

void
init_graph(GraphFrame *gf)
{
  set_dimensions(gf, WIDTH, HEIGHT);
  set_show_vertex_weight(gf, False);
  set_show_edge_weight(gf, False);
  set_shape(gf, BOX);
  set_dim_vertex(gf,BSIDE,HSIDE);
  set_scale(gf, 0);
  set_flag_draw(gf, True);
  set_num_vertex(gf, 0);
  set_num_ver_group(gf, 0);
  set_count_vertex(gf,0);
  set_method_tree(gf,BFS_TREE);
  set_graph_dir(gf, NONE);
}

int 
read_regular_file(char *filename, char **stext)
{
  char *text;
  struct stat statb;
  FILE *fp;

  *stext = NULL;

  /* make sure the file is a regular text file and open it */
  if(stat(filename, &statb) == -1 ||
     (statb.st_mode & S_IFMT ) != S_IFREG ||
     !(fp = fopen(filename, "r"))) 
    {
      if((statb.st_mode & S_IFMT) == S_IFREG)
	perror(filename);
      else
	fprintf(stderr, "%s : not a regular file \n", filename);
      free(filename); filename = NULL;
      return -1;
    }

  if(!(text = malloc((unsigned)(statb.st_size +1))))
    {
      fprintf(stderr, "Can't alloc enough space for %s", filename);
      free(filename); filename = NULL;
      fclose(fp);
      abort();
    }
  if(!fread(text,sizeof(char), statb.st_size + 1, fp))
    fprintf(stderr, "Warning: may not have read entire file!\n");
  text[statb.st_size] = 0; /* be sure to NULL-terminate */
  *stext = text;
  fclose(fp);
  return 0;
}


char *
get_base_name(char *filename)
{
  int i = 0;
  int len = strlen(filename);
  int start=0, end=len;

  for(i = len - 1; i >= 0 ; i--)
    {
      if(filename[i]=='.')
	{
	  end = i;
	  break;
	}
      
    }
  for(i = len - 1; i >= 0 ; i--)
    {
      if(filename[i]=='/')
	{
	  start = i+1;
	  break;
	}
    }

  if(end > start)
    {
      int j=0;
      char *tstr = (char *)malloc((end - start + 2)* sizeof(char));
      if(!tstr) {
	  fprintf(stderr,"Out of Memory get_base_name\n");
	  abort();
      }
      for(i= start ; i < end ;i++)
	{
	  tstr[j++] = filename[i];
	}
      tstr[j] = '\0';
      return tstr;
    }
  return NULL;
}

#define SHAPE_HTTP_HTML_DOC BOX
#define SHAPE_HTTP_ANY_DOC CIRCULAR
#define SHAPE_HTTP_DOC OCTAGON
#define SHAPE_FTP_DOC RHOMBUS
#define SHAPE_MAILTO_DOC HOR_ELLIPSIS
#define SHAPE_PROT_DOC VER_ELLIPSIS
#define SHAPE_BROKEN_DOC TRIANGLE

void
set_shape_from_attributes(GraphFrame *gf)
{
  LNode_type *ptr, *lv = get_list_vertices(gf);
  for(ptr = lv->back  ; ptr->back !=  lv ; ptr = ptr->back)
    {
      struct pt *v = (struct pt * )ptr->info;
      char *label = get_label_vertex(v);
      const char *mtype = get_attribute(v, "mime");
      const char *msize =get_attribute(v,"size");
      int size = msize ? atoi(msize) : -1;
      const char *mcode =get_attribute(v,"code");
      int code = mcode ? atoi(mcode) : -1;

       if(!strncmp(label,"http://",7))
	 {
	   if(mtype && !strcmp(mtype,"text/html"))
	     set_vertex_shape(v,SHAPE_HTTP_HTML_DOC);
	   else if(mtype && !strcmp(mtype,"www/unknown"))
	     set_vertex_shape(v,SHAPE_HTTP_HTML_DOC);
	   else if(size >=0)
	     set_vertex_shape(v,SHAPE_HTTP_ANY_DOC);
	   else
	     set_vertex_shape(v,SHAPE_HTTP_DOC);
	 }
       else if(!strncmp(label,"ftp://",6))
	 set_vertex_shape(v,SHAPE_FTP_DOC);
       else if(!strncmp(label,"mailto:",7))
	 set_vertex_shape(v,SHAPE_MAILTO_DOC);
       else
	 set_vertex_shape(v,SHAPE_PROT_DOC);

       if(code > 400)
	 set_vertex_shape(v,SHAPE_BROKEN_DOC);
    }
}


